import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/store'
import { Message } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import * as mutations from "@/store/mutations";

const Login = () => import('../views/login/Login.vue')
const Welcome = () => import('../views/welcome/Welcome.vue')
const Register = () => import('../views/register/Register.vue')
const Problems_list= () => import('../views/problems_list/Problems_list.vue')
const Problem_detail= () => import('../views/problem_detail/Problem_detail.vue')
const Code_page= () => import('../views/code_page/Code_page.vue')
const Chat_room= () => import('../views/chat_room/Chat_room.vue')
const Message_board = () => import('../views/message_board/Message_board.vue')
const Interview = () => import('../views/interview/Interview.vue')
const Interviewer_room= () => import('../views/interview_room/interviewer_room.vue')
const Interviewee_room= () => import('../views/interview_room/interviewee_room.vue')
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      component: Welcome,
      meta: {
        requiresAuth: false,
        requireIdentity:false,
      }
    },

    {
      path: '/welcome',
      component: Welcome,
      meta: {
        requiresAuth: false,
        requireIdentity:false,
      }
    },

    {
      path: '/problems_list',
      component: Problems_list,
      meta: {
        requiresAuth: false,
        requireIdentity:false,
      }
    },
    {
      path: '/login',
      component: Login,
      meta: {
        requiresAuth: false,
        requireIdentity:false,
      }
    },
    {
      path: '/message_board',
      component: Message_board,
      meta: {
        requiresAuth: true,
        requireIdentity:false,
      }
    },
    {
      path: '/problem',//带参数
      name: 'problem',
      component: Problem_detail,
      meta: {
        requiresAuth: true,
        requireIdentity:false,
      }
    },
    {
      path: '/code_page',
      component: Code_page,
      meta: {
        requiresAuth: false,
        requireIdentity:false,
      }
    },
    {
      path: '/chat_room',
      component: Chat_room,
      meta: {
        requiresAuth: true,
        requireIdentity:false,
      }
    },
    {
      path: '/interview',
      component: Interview,
      meta: {
        requiresAuth: true,
        requireIdentity:false,
      }
    },
    {
      path: '/interviewee_room',
      component: Interviewee_room,
      meta: {
        requiresAuth: true,
        requireIdentity:true,

      }
    },
    {
      path: '/interviewer_room',
      component: Interviewer_room,
      meta: {
        requiresAuth: true,
        requireIdentity:true,
      }
    },


  ],
  mode: 'history'
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(r => r.meta.requiresAuth)) {  // 判断该路由是否需要登录权限
    store.commit('checkLogin');
    //console.log(store.getters.isLogin);
    if (store.getters.isLogin===true) {  // 通过vuex 如果当前有登录

      next();
    }
    else {
      Message.error("您还没有登录，请先登录")
    }
  }
  else {
    next();
  }
});


export default router